﻿using System.Globalization;
using Telerik.Web.UI;

/// <summary>
/// RadGrid数据绑定
/// </summary>
public class RadGridBinder
{
    private SqlPager pager;
    private RadGrid grid;

    private RadGridBinder(RadGrid grid, SqlPager pager)
    {
        this.grid = grid;
        this.pager = pager;        
        grid.AllowPaging = true;
        grid.AllowCustomPaging = true;        
        grid.AllowSorting = true;
        grid.AllowMultiRowSelection = true;
        grid.AutoGenerateColumns = false;
        grid.Culture = CultureInfo.CurrentCulture;

        grid.PagerStyle.AlwaysVisible = true;

        grid.ClientSettings.AllowColumnsReorder = true;
        grid.ClientSettings.ReorderColumnsOnClient = true;
        grid.ClientSettings.Selecting.AllowRowSelect = true;
        grid.ClientSettings.Resizing.AllowColumnResize = true;

        grid.NeedDataSource += grid_NeedDataSource;
        grid.PageIndexChanged += grid_PageIndexChanged;
        grid.PageSizeChanged += grid_PageSizeChanged;
        grid.SortCommand += grid_SortCommand;
    }

    /// <summary>
    /// 默认获取数据的事件
    /// 当调用grid.ReBind()方法时也会调用此事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    void grid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        BindData();
    }

    /// <summary>
    /// 当前页号变更事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    void grid_PageIndexChanged(object source, GridPageChangedEventArgs e)
    {
        BindData(e.NewPageIndex, grid.PageSize, GetSortOrder());
    }

    /// <summary>
    /// 每页数据条数变更的事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    void grid_PageSizeChanged(object source, GridPageSizeChangedEventArgs e)
    {
        BindData(grid.CurrentPageIndex, e.NewPageSize, GetSortOrder());
    }

    /// <summary>
    /// 排序事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    void grid_SortCommand(object source, GridSortCommandEventArgs e)
    {
        string sorder = e.SortExpression;
        if (e.NewSortOrder == GridSortOrder.Descending)
            sorder += " desc";
        BindData(grid.CurrentPageIndex, grid.PageSize, sorder);
    }

    /// <summary>
    /// 获取当前的排序
    /// </summary>
    /// <returns></returns>
    string GetSortOrder()
    {
        GridSortExpressionCollection gscollection = grid.MasterTableView.SortExpressions;
        string sorder = string.Empty;
        if (gscollection != null && gscollection.Count > 0)
        {
            sorder = gscollection.GetSortString();
        }
        return sorder;
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    public void BindData()
    {
        BindData(grid.CurrentPageIndex, grid.PageSize, GetSortOrder());
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="pageIndex">页号</param>
    /// <param name="pageSize">每页数据条数</param>
    /// <param name="order">排序</param>
    void BindData(int pageIndex, int pageSize, string order)
    {
        pager.PageSize = pageSize;
        pager.PageIndex = pageIndex + 1;
        if (!string.IsNullOrEmpty(order))
            pager.OrderBy = order;
        string sql = pager.GetPagedSql();
        grid.VirtualItemCount = pager.RecordCount;
        grid.DataSource = SqlHelper.Instance./*ExecuteReader(sql)*/ExecuteDataset(sql).Tables[0];
    }

    public static RadGridBinder Create(RadGrid grid, string tableName, string identityField, string fieldNames)
    {
        return Create(grid, new SqlPager(tableName, identityField, fieldNames));
    }

    public static RadGridBinder Create(RadGrid grid, string tableName, string IdentityField)
    {
        return Create(grid, new SqlPager(tableName, IdentityField, "*"));
    }

    public static RadGridBinder Create(RadGrid grid, SqlPager pager)
    {
        return new RadGridBinder(grid, pager);
    }
}